import AiBlob
from opencv import cv
import sys
sys.path.append("../informationExtraction_modules")
from AiNamedEntity import keywords


class AiPage:
    def __init__(self, pageNr, aiimage, text, parentbook):
        self.nr = pageNr
        self.image = aiimage
        self.text = text
        self.parentbook = parentbook
        self.blobimage = None
        self.tagimage = None
        self.objectWords = None

    def findWords(self):
        # TODO: Hardy, put your stuff here...
        # Original pagetext in self.text
        self.objectWords = keywords(self.text)

    def findBlobs(self):
        # Find blobs
        self.blobs = AiBlob.findBlobs(self.image, self.nr)
        
        # Add reference to page
        for blob in self.blobs:
            blob.page = self

        # Create image for GUI
        img = self.image.clone()
        font = cv.cvInitFont(cv.CV_FONT_HERSHEY_PLAIN, 1.0, 1.0)
        for blob in self.blobs:
            cv.cvRectangle(img.img, (blob.imgRegion.x,blob.imgRegion.y),\
                           (blob.imgRegion.x+blob.imgRegion.width,\
                           blob.imgRegion.y+blob.imgRegion.height),\
                           cv.CV_RGB(255,0,0), 2)
            cv.cvPutText(img.img, str(blob.id), (blob.imgRegion.x,\
                         blob.imgRegion.y + blob.imgRegion.height),\
                         font, cv.CV_RGB(0,0,0))
        self.blobimage = img

    def generateTags(self):
        img = self.image.clone()
        font = cv.cvInitFont(cv.CV_FONT_HERSHEY_PLAIN, 1.0, 1.0)
        for blob in self.blobs:
            cv.cvRectangle(img.img, (blob.imgRegion.x,blob.imgRegion.y),\
                           (blob.imgRegion.x+blob.imgRegion.width,\
                           blob.imgRegion.y+blob.imgRegion.height),\
                           cv.CV_RGB(255,0,0), 2)
            if blob.tag is not None:
                cv.cvPutText(img.img, str(blob.id) + "-" + str(blob.tag),\
                         (blob.imgRegion.x, blob.imgRegion.y + \
                         blob.imgRegion.height), font, cv.CV_RGB(0,0,0))
            else:
                cv.cvPutText(img.img, str(blob.id), (blob.imgRegion.x,\
                         blob.imgRegion.y + blob.imgRegion.height),\
                         font, cv.CV_RGB(0,0,0))
        self.tagimage = img

